<template>
  <view class="goods-item">
    <!-- 左侧的盒子 -->
    <view class="goods-item-left">
      <radio :checked="item.goods_state" color="#C00000" v-if="showRadio" @click="radioChangHandler"></radio>
      <image class="image-goods" :src="item.goods_big_logo?item.goods_big_logo:defaultPic" mode="widthFix">
      </image>
    </view>
    <!-- 右侧的盒子 -->
    <view class="goods-item-right">
      <view class="goods_name">{{item.goods_name}}</view>
      <view class="box">
        <text>￥{{tofixed(item.goods_price)}}</text>
        <uni-number-box :min="1" :value="item.goods_count" v-if="showBox" @change="numberChange"></uni-number-box>
      </view>
    </view>
  </view>
</template>
<script>
  export default {
    props: {
      item: {
        type: Object,
        default: {}
      },
      //控制商品是否展示勾选的圈圈
      showRadio: {
        type: Boolean,
        default: false //默认隐藏
      },
      //控制NumberBox的显示与隐藏
      showBox: {
        type: Boolean,
        default: false //默认隐藏
      }
    },

    data() {
      return {
        defaultPic: 'https://ts1.cn.mm.bing.net/th/id/R-C.06129c7d184f826a8f51a5aa3c315d3e?rik=vtAgTDd%2f16aKNA&riu=http%3a%2f%2fbpic.588ku.com%2felement_pic%2f01%2f47%2f02%2f5857433906edc48.jpg&ehk=GPhWys5KxyP%2bOfCeFfYzTqGm0FF7sgA3qK2%2fw2DPlts%3d&risl=&pid=ImgRaw&r=0'
      }
    },
    methods: {
      //这是radio 组件的 点击事件处理函数
      radioChangHandler() {
        //子传父，传到父亲cart.vue中去
        this.$emit('radio-change', {
          goods_id: this.item.goods_id,
          goods_state: !this.item.goods_state
        })
      },
      //保留两位小数
      tofixed(num) {
        return Number(num).toFixed(2)
      },
      //监听了uni-number-box 组件：数量改变的提交
      numberChange(val) {
        console.log(val);
        this.$emit('num-change', {
          goods_id: this.item.goods_id,
          goods_count: val
        })
      }

    },
    mounted() {
      console.log(this.item.goods_big_logo); // 调试输出
    }

  }
</script>

<style lang="scss">
  .goods-item {
    display: flex;
    padding: 0 5px;
    margin: 2px 0;

    .goods-item-left {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .image-goods {
        width: 100px;
        height: 100px;
        display: block;
      }
    }

    .goods-item-right {
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .box {
        color: red;
        display: flex;
        flex-wrap: 1;
        justify-content: space-between;
        align-items: center;
      }
    }
  }
</style>